<template>
	<view class="login">
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		<view class="top headline">
			<uni-icons type="arrowleft" size="24" class="left" color="#fff" @click="back()" />
		</view>
		<view class="bg">
			<image src="/static/img/bg_login.png" mode="widthFix" />
		</view>
		<view class="box">
			<view class="input">
				<image src="/static/img/phone1.png" />
				<input type="number" placeholder="请输入手机号" v-model="input.username" AUTOCOMPLETE="off" maxlength="11" />
			</view>
			<view class="input">
				<image src="/static/img/password1.png" />
				<input type="password" placeholder="请输入密码" v-model="input.password" AUTOCOMPLETE="off" maxlength="16" />
			</view>
			<view class="button" @click="go_login">登 录</view>
			<view class="operation">
				<text class="left" @click="forgetAccount">忘记密码？</text>
				<text class="right" @tap="registerNewAccount">注册账号</text>
			</view>
		</view>
	</view>
</template>
<script>
	import login from '../../../servers/shop/login.js';
	import my from '../../../servers/shop/my.js';
	import {
		uniIcons
	} from '@dcloudio/uni-ui';
	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				input: {
					username: '',
					password: '',
					code: ''
				},
				compile: false,
				is_go_top: false,
				is_all: false
			};
		},
		onBackPress: function(e) {
			this.back()
			return true
		},
		mounted() {
			if (this.$jwx.isWechat()) {
				if (!window.location.search || window.location.search.split('&')[0].indexOf("code=") == -1) {
					window.location.href =
						"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2b1b67a124d7511f&redirect_uri=http%3A%2F%2Flm.weixingshishang.cn%2F%23%2Fpages%2Fshop%2Faccount%2Flogin&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
				} else {
					this.input.code = window.location.search.split('&')[0].split('=')[1]
				}
			}
		},
		methods: {
			back() {
				uni.switchTab({
					url: '/pages/shop/index'
				});
			},
			registerNewAccount() {
				uni.navigateTo({
					url: './register'
				});
			},
			// 登陆 15963235698
			go_login() {
				if (!this.input.username) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号',
						duration: 2000
					});
					return;
				}
				if (!this.input.password) {
					uni.showToast({
						icon: 'none',
						title: '请输入密码',
						duration: 2000
					});
					return;
				}
				if (this.input.password.length < 6) {
					uni.showToast({
						title: "请输入6及位数以上密码！",
						icon: 'none',
						position: 'bottom'
					})
					return
				}
				var auth = this.encode('weixing-mall-mem:mem-client-security');
				// #ifndef H5
				var url1 = this.AppUrl + '/api/uaa/member/login'
				// #endif
				// #ifdef H5
				var url1 = '/api/uaa/member/login'
				// #endif
				uni.showLoading({
					title: '登录中',
					icon: 'loading',
					mask: true
				});
				uni.request({
					url: url1, //仅为示例，并非真实接口地址。
					data: this.input,
					method: 'Post',
					header: {
						'Authorization': 'Basic ' + auth,
						'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
					},
					success: res => {
						if (res.data.code != 200 || res.data.subCode) {
							uni.hideToast();
							uni.showToast({
								icon: 'none',
								title: res.data.msg,
								duration: 2000
							});
						}
						if (res.data.code == 200) {
							var obj = new Object();
							obj.access_token = res.data.data.access_token;
							// ur = r2 会员
							// ur = r3 管理员
							// mr= "a" 代理商
							// mr= "m" 会员
							uni.setStorageSync('Authorization', 'Bearer ' + res.data.data.access_token);
							uni.setStorageSync('refreshToken', res.data.data.refresh_token);
							uni.setStorageSync('expires_in', res.data.data.expires_in);


							uni.setStorageSync('key_time', Date.parse(new Date()));
							uni.setStorageSync('ur', res.data.data.ur);
							uni.setStorageSync('mr', res.data.data.mr);

							login.detail().then(resa => {
								console.log(resa)
								uni.hideToast();
								uni.setStorageSync('login', resa.data);
								uni.switchTab({
									url: '/pages/shop/index'
								});
							});
							if (res.data.data.mr == 'a') {
								my.agentinfo()
									.then(res => {
										if (res.code == 200) {
											// _this.initData = res.data;
											console.log(res, res.data)
											uni.hideLoading();

											uni.setStorageSync('agentinfo', JSON.stringify(res.data))
										} else {
											// uni.showToast({
											// 	title:res.msg,
											// 	icon:'none'
											// })
										}

									})
									.catch(err => {

										console.log(new Error('请检查网络后重试'));
									});
							}

						}
					},
					fail: complete => {
						console.log(complete)
						uni.hideToast();
						uni.showToast({
							icon: 'none',
							title: '登录失败，请检查网络连接',
							duration: 2000
						});

					if (res.data.code == 200) {
						var obj = new Object();
						obj.access_token = res.data.data.access_token;
						// ur = r2 会员
						// ur = r3 管理员
						// mr= "a" 代理商
						// mr= "m" 会员
						uni.setStorageSync('Authorization', 'Bearer '+res.data.data.access_token);
						uni.setStorageSync('refreshToken', res.data.data.refresh_token);
						uni.setStorageSync('expires_in', res.data.data.expires_in);
						uni.setStorageSync('key_time', Date.parse(new Date()));
						uni.setStorageSync('ur', res.data.data.ur);
						uni.setStorageSync('mr', res.data.data.mr);
						
						login.detail().then(resa => {
							console.log(resa)
							uni.hideToast();
							uni.setStorageSync('login', resa.data);
							uni.switchTab({
								url: '/pages/shop/index'
							});
						});
						if(res.data.data.mr=='a'){
							my.agentinfo()
								.then(res => {
									if(res.code==200){
										// _this.initData = res.data;
										console.log(res,res.data)
										uni.hideLoading();
										
										uni.setStorageSync('agentinfo',JSON.stringify(res.data))
									}else{
										// uni.showToast({
										// 	title:res.msg,
										// 	icon:'none'
										// })
									}
									
								})
								.catch(err => {
									
									console.log(new Error('请检查网络后重试'));
								});
						}
						
					}
				},
				fail:complete=> {
					console.log(complete)
					uni.hideToast();
					uni.showToast({
						icon: 'none',
						title: '登录失败，请检查网络连接',
						duration: 2000
					});
					
					
				}
			});
		},
		forgetAccount() {
			uni.navigateTo({
				url: './forget'
			});
		},
		encode(str) {
		   // 对字符串进行编码
		   var encode = encodeURI(str);
		   // 对编码的字符串转化base64
		   var base64 = btoa(encode);
		   return base64;
		  }
	}
};
</script>
<style lang="less" scoped>
	.login {
		.status_bar {
			background: #bd3c8c;
		}

		.headline {
			position: fixed;
			border-bottom: none;
			background: none;
			color: #fff;
		}

		.bg {
			image {
				width: 100%;
				// height: 688rpx;
			}
		}

		.box {
			position: relative;
			background: #fff;
			border-radius: 30rpx;
			width: 683rpx;
			margin: 0 auto;
			margin-top: -165rpx;
			padding: 94rpx 33rpx;
			box-sizing: border-box;

			.input {
				position: relative;
				background: #f4f4f4;
				border-radius: 10rpx;
				overflow: hidden;
				margin-bottom: 70rpx;

				image {
					position: absolute;
					width: 31rpx;
					height: 43rpx;
					top: 25rpx;
					left: 27rpx;
				}

				input {
					background: #f4f4f4;
					width: 100%;
					height: 86rpx;
					line-height: 100rpx;
					padding-left: 88rpx;
					font-size: 24rpx;
					box-sizing: border-box;
				}
			}

			.button {
				line-height: 90rpx;
				background: linear-gradient(80deg, #d2158a, #fc6395);
				text-align: center;
				color: #fff;
				border-radius: 45rpx;
				font-size: 32rpx;
				box-shadow: 3rpx 0 20rpx rgba(206, 205, 204, 0.35);
			}

			.operation {
				margin-top: 56rpx;
				font-size: 24rpx;
			}
		}
	}
</style>
